const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/nodeData';
const bodyParser = require('body-parser');
const passport = require('passport');
const app = express();
//引入users.js
const users = require('./routes/api/users');
const profile = require('./routes/api/profile');

//使用body-parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
//passport 初始化
app.use(passport.initialize());
//引入
require('./config/passport')(passport);


//登录注册路由
app.use('/api/users',users);
//表格
app.use('/api/profile',profile);

//查询数据库  是否已经创建数据库
MongoClient.connect(url,{useNewUrlParser:true},function (err, db) {
    if (err) throw err;
    console.log('数据库已创建');
    var dbase = db.db("nodeData");
    dbase.createCollection('site', function (err, res) {
        if (err) throw err;
        console.log("创建集合!");
        db.close();
    });
});


const port = process.env.port || 5000;
app.listen(port, () => {
    console.log(`Server port ${port}`)
})